////////////////////////////////////////////////////////////////////////////////////////
//
// JSimpleSim - A machine language simulator based on SimpSim by Anne-Gert Bultena.
//
// Copyright (C) 2008 Gerardo Camorlinga, Ryan Fitzgerald, and RJ Gordon
//
// This file is part of JSimpleSim.
//
// JSimpleSim is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// JSimpleSim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with JSimpleSim.  If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////////////

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsimplesim.view.components;

import jsimplesim.view.*;
import jsimplesim.view.renderers.RegRedCellRenderer;
import jsimplesim.view.renderers.HexCellRenderer;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
import org.apache.log4j.Logger;
import jsimplesim.model.RegHexTableModel;

/**
 * 
 * @author f08maroon
 */
@SuppressWarnings("serial")
public class RegisterTable extends JTable implements TableModelListener {

    private TableColumn column;
    @SuppressWarnings("unused")
	private JSimpleSimView view;
    static Logger logger = Logger.getLogger(RegisterTable.class);

    /**
     *
     * @param model
     * @param view1
     */
    public RegisterTable(RegHexTableModel model, JSimpleSimView view1) {
        super(model);
        this.view = view1;

        setFont(new Font("monospaced", Font.PLAIN, 12));
        setShowGrid(true);
        setRowSelectionAllowed(false);
        setIntercellSpacing(new Dimension(0, 0));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        setAutoscrolls(false);

        setColumnSelectionAllowed(false);

        setName("memoryTable"); // NOI18N

        setRowHeight(19);

        getTableHeader().setReorderingAllowed(false);
        getColumnModel().getSelectionModel().setSelectionMode(
                javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        // addKeyListener(this);

        // Set the size of each column

        int characterWidth = getFontMetrics(getFont()).charWidth('w');
        int columns = getModel().getColumnCount();
        this.getModel().addTableModelListener(this);

        for (int i = 0; i < columns; i++) {
            column = getColumnModel().getColumn(i);

            if (i == 0) {
                column.setPreferredWidth(9 * characterWidth);
            } else {
                column.setPreferredWidth(3 * characterWidth);
            }
        }

        // Set default cell renderer

        TableCellRenderer hexCellRenderer = new HexCellRenderer();
        setDefaultRenderer(String.class, hexCellRenderer);

        // Set default cell editor

        HexCellEditor hce = new HexCellEditor();
        hce.setFont(getFont());
        DefaultCellEditor dce = new DefaultCellEditor(hce);
        dce.setClickCountToStart(1);
        setDefaultEditor(String.class, dce);

    }

    @SuppressWarnings("unused")
	@Override
    public void updateUI() {
        //System.out.println("RegisterTable updateUI called");
        super.updateUI();
        int columnCount = getColumnCount();
        TableColumnModel columnModel = getColumnModel();
        columnModel.getColumn(0).setCellRenderer(new RegRedCellRenderer());

    }
}
